<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .list{
            width: 200px;
            height: 150px;
            background-color: skyblue;
            overflow:auto;
        }
        .news{
            height: 30px;
        }
    </style>
</head>
<body>
    <div id="test"></div>
    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
    <script type="text/babel">
        class Test extends React.Component {
            state={newsArr:['新闻1']}
            list = React.createRef()
            componentDidMount(){
                setInterval(()=>{
                    const {newsArr} = this.state
                    const news = '新闻'+(newsArr.length+1)
                    this.setState({newsArr:[news,...newsArr]})
                },1000)
            }
            getSnapshotBeforeUpdate(){
                console.log(this)
                return null
            }
            componentDidUpdate(preProps,preState,value){
                if(this.list)this.list.scrollTop += this.list.scrollHeight - value 
                console.log(this.list)
            }
            render() { 
                return  (
                    <div className="list" ref={this.list}>
                        {
                            this.state.newsArr.map((n,index)=> <div className="news" key={index}>{n}</div>)
                        }
                    </div>
                )
            }
        }
        ReactDOM.render(<Test />, document.getElementById("test"))
    </script>
</body>
</html>